{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 创建文件并写入\n",
    "f = open('./res/file01.txt', 'w')\n",
    "f.write(\"Hello, World\")\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [
    "# 缺省情况文件显示为乱码\n",
    "f = open('./res/file01.txt', 'w')\n",
    "f.write(\"你好！Python\")\n",
    "f.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "# 以UTF-8编码方式打开文件\n",
    "f = open('./res/file01.txt', 'w', encoding='UTF-8')\n",
    "f.write(\"你好！Python\")\n",
    "f.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "# 写入多行文件(方式一)\n",
    "f = open('./res/file01.txt', 'w', encoding='UTF-8')\n",
    "f.write(\"你好！Python\\n你好！Python\\n你好！Python\\n你好！Python\")\n",
    "f.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "# 写入多行文件(方式一)\n",
    "f = open('./res/file01.txt', 'w', encoding='UTF-8')\n",
    "f.writelines(['你好！Python\\n', '你好！Python\\n', '你好！Python\\n', '你好！Python\\n'])\n",
    "f.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！Python\n",
      "你好！Python\n",
      "你好！Python\n",
      "你好！Python\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 文件读取，读取整个文件\n",
    "f = open('./res/file01.txt', 'r', encoding='utf-8')\n",
    "contents = f.read()\n",
    "f.close()\n",
    "\n",
    "print(contents)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！Pyt\n"
     ]
    }
   ],
   "source": [
    "# 安装指定字符数读取文件\n",
    "f = open('./res/file01.txt', 'r', encoding='utf-8')\n",
    "contents = f.read(6)\n",
    "f.close()\n",
    "\n",
    "print(contents)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！Python\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 按行读取文件\n",
    "f = open('./res/file01.txt', 'r', encoding='utf-8')\n",
    "while True:\n",
    "    contents = f.readline()\n",
    "f.close()\n",
    "\n",
    "print(contents)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['你好！Python\\n', '你好！Python\\n', '你好！Python\\n', '你好！Python\\n']\n",
      "['你好！Python\\n']\n"
     ]
    }
   ],
   "source": [
    "# 读取多行文件\n",
    "f = open('./res/file01.txt', 'r', encoding='utf-8')\n",
    "contents = f.readlines()\n",
    "f.close()\n",
    "\n",
    "print(contents)\n",
    "\n",
    "# 也可以读指定的行\n",
    "\n",
    "f = open('./res/file01.txt', 'r', encoding='utf-8')\n",
    "contents = f.readlines(2)\n",
    "f.close()\n",
    "\n",
    "print(contents)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}